package com.zmj.sy.mom.srv.aps.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zmj.sy.mom.srv.aps.bean.entity.order.*;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.orderMonitor.*;
import com.zmj.sy.mom.srv.aps.config.ProjectConfig;
import com.zmj.sy.mom.srv.aps.feign.WmsFeignApiClient;
import com.zmj.sy.mom.srv.aps.mapper.*;
import com.zmj.sy.mom.srv.aps.utils.SyExceptionUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class OrderMonitorService {

    private final OrderMapper orderMapper;

    private final ApsWorkOrderMapper apsWorkOrderMapper;

    private final ApsWorkDetailMapper apsWorkDetailMapper;

    private final PartPalletMapper partPalletMapper;

    private final SinoCamReceiveNestPartMapper sinoCamReceiveNestPartMapper;
    private final OrderBomMapper orderBomMapper;

    private final WmsFeignApiClient wmsFeignApiClient;
    private final ProjectConfig projectConfig;
    private final OrderPartMapper orderPartMapper;

    public List<Map<String, Object>> getList(Map<String, Object> mapReq) {

        Map<Integer, Order> orders;

        if (mapReq == null) {
            orders = orderMapper.lambdaQuery()
                    .eq(Order::getOrderType, 1)
                    .ge(Order::getOrderStatus, 6)
                    .orderByDesc(Order::getCreateDateTime)
                    .last("limit 1")
                    .list().stream()
                    .collect(Collectors.toMap(Order::getId, Function.identity()));
        } else {
            if (!ObjectUtil.isEmpty(mapReq.get("orderCode"))) {
                // 查询施工单下的订单（输入施工号时）
                orders = orderMapper.lambdaQuery()
                        .like(Order::getConstructNo, String.valueOf(mapReq.get("orderCode")))
                        .list().stream()
                        .collect(Collectors.toMap(Order::getId, Function.identity()));
                if (orders.isEmpty()) {
                    throw SyExceptionUtils.e("未查询到该施工号的订单", String.valueOf(mapReq.get("orderCode")));
                }
            } else {
                if (!ObjectUtil.isEmpty(mapReq.get("plmid"))) {
                    // 查询包含输入零件号的订单（不输入施工号，但输入零件号时）
                    List<OrderBom> orderBom = orderBomMapper.lambdaQuery()
                            .like(OrderBom::getPlmId, String.valueOf(mapReq.get("plmid")))
                            .list();
                    if (orderBom.isEmpty()) {
                        throw SyExceptionUtils.e("未查询到包含该零件的信息", String.valueOf(mapReq.get("plmid")));
                    }
                    orders = orderMapper.lambdaQuery()
                            .in(Order::getId, orderBom.stream().map(OrderBom::getOrderId).collect(Collectors.toList()))
                            .list().stream()
                            .collect(Collectors.toMap(Order::getId, Function.identity()));
                } else {
                    // 查询最新施工单下的订单（不输入施工号，且不输入零件号时）
                    orders = orderMapper.lambdaQuery()
                            .eq(Order::getOrderType, 1)
                            .ge(Order::getOrderStatus, 6)
                            .orderByDesc(Order::getCreateDateTime)
                            .last("limit 1")
                            .list().stream()
                            .collect(Collectors.toMap(Order::getId, Function.identity()));
                }
            }
        }
        if (orders.isEmpty()) { // 无订单
            return new ArrayList<>();
        }

        // 查想订单相关的工单
        Map<Integer, ApsWorkOrder> workOrders = apsWorkOrderMapper.lambdaQuery()
                .in(ApsWorkOrder::getOrderId, orders.keySet())
                .list().stream()
                .collect(Collectors.toMap(ApsWorkOrder::getId, Function.identity()));
        // 查询工单明细并进行聚合
        List<Map<String, Object>> result = new ArrayList<>();
        Map<List<String>, List<ApsWorkDetail>> map = apsWorkDetailMapper.lambdaQuery()
                .in(ApsWorkDetail::getOrderId, orders.keySet())
                .like(!ObjectUtil.isEmpty(mapReq.get("plmid")), ApsWorkDetail::getMaterNo, String.valueOf(mapReq.get("plmid")))
                .list().stream()
                .filter(detail -> orders.containsKey(detail.getOrderId())
                        && workOrders.containsKey(detail.getApsWorkOrderId()))
                .collect(Collectors.groupingBy(detail -> {
                    Order order = orders.get(detail.getOrderId());
                    ApsWorkOrder workOrder = workOrders.get(detail.getApsWorkOrderId());
                    return Arrays.asList(order.getConstructNo(), workOrder.getWorkOrderCode(), detail.getMaterNo());
                }, Collectors.toList()));
        for (List<ApsWorkDetail> groupDetails : map.values()) {
            Map<String, Object> item = getInitMap();
            ApsWorkDetail detail = groupDetails.get(0);
            Order order = orders.get(detail.getOrderId());
            ApsWorkOrder workOrder = workOrders.get(detail.getApsWorkOrderId());
            item.put("id", detail.getId());
            item.put("orderCode", order.getConstructNo());
            item.put("workOrderCode", workOrder.getWorkOrderCode());
            item.put("plmid", detail.getMaterNo());
            item.put("materName", detail.getMaterName());
            ApsWorkOrder parentWorkOrder = workOrders.get(workOrder.getPid());
            Map<String, List<ApsWorkDetail>> phaseDetailMap = groupDetails.stream()
//                    .sorted(Comparator.comparing(ApsWorkDetail::getPhaseSeq))
                    .collect(Collectors.groupingBy(ApsWorkDetail::getPhaseCode, Collectors.toList()));
            BigDecimal materialFinishCount = BigDecimal.ZERO;
            BigDecimal materialTotalCount = BigDecimal.ZERO;
            // 获取最大工序序号的计划
            ApsWorkDetail max = groupDetails.stream().max(Comparator.comparing(e -> e.getPhaseSeq())).get();
//            List<ApsWorkDetail> list = new ArrayList<>(phaseDetails);
//            Collection<ApsWorkDetail> values = list.stream().filter(e -> e.getPhaseSeq().equals(apsWorkDetail.getPhaseSeq())).collect(Collectors.toMap(e -> e.getPhaseSeq(), e -> e, (e1, e2) -> {
//                e1.setTotalCount(e1.getTotalCount().add(e2.getTotalCount()));
//                e1.setFinishCount(e1.getFinishCount().add(e2.getFinishCount()));
//                return e1;
//            })).values();
//            ApsWorkDetail workDetail = new ArrayList<>(values).get(0);

            ApsWorkDetail last = null;

            for (List<ApsWorkDetail> phaseDetails : phaseDetailMap.values()) {

                // 获取最大工序序号的计划
                ApsWorkDetail apsWorkDetail = phaseDetails.stream().max(Comparator.comparing(e -> e.getPhaseSeq())).get();
                // 不修改原List，创建新List赋值
                List<ApsWorkDetail> list = new ArrayList<>(phaseDetails);
                // 按工序的序号，将数量进行累加，合并到第一条数据上
                Collection<ApsWorkDetail> values = list.stream().filter(e -> e.getPhaseSeq().equals(apsWorkDetail.getPhaseSeq())).collect(Collectors.toMap(e -> e.getPhaseSeq(), e -> e, (e1, e2) -> {
                    e1.setTotalCount(e1.getTotalCount().add(e2.getTotalCount()));
                    e1.setFinishCount(e1.getFinishCount().add(e2.getFinishCount()));
                    return e1;
                })).values();
                // 获取其中的第一条数据（只有一条，但是类型是Collection，强转为List后获取第一条）
                ApsWorkDetail workDetail = new ArrayList<>(values).get(0);

                if (Objects.equals(workDetail.getPhaseSeq(), max.getPhaseSeq())) {
                    // 如果是工序序号最大的一个，则赋值给last，用于零件或组件的订单完成率
                    last = workDetail;
                    materialFinishCount = last.getFinishCount();
                    materialTotalCount = last.getTotalCount();
                }
                // 获取零件或组件的各个工序完成数量和总数量
                BigDecimal finishCount = workDetail.getFinishCount();
                BigDecimal totalCount = workDetail.getTotalCount();
//                for (ApsWorkDetail phaseDetail : phaseDetails) {
//                    if (phaseDetail.getFinishCount() != null) {
//                        finishCount = finishCount.add(phaseDetail.getFinishCount());
//                    }
//                    totalCount = totalCount.add(phaseDetail.getTotalCount());
//                }
//                materialFinishCount = finishCount;
//                materialTotalCount = totalCount;
                item.put(phaseDetails.get(0).getPhaseCode(), String.format("%s/%s", finishCount.setScale(0, RoundingMode.HALF_UP), totalCount.setScale(0, RoundingMode.HALF_UP)));

            }
            // 获取图纸信息
            OrderPart orderPart = orderPartMapper.lambdaQuery().eq(OrderPart::getPlmId, detail.getMaterNo()).one();
            String partImage = null;
            if (orderPart != null && orderPart.getPngUrl() != null) {
                String partPlmId = orderPart.getPlmId();
                String url = orderPart.getPngUrl();
                partImage = String.format(projectConfig.getUrlPre(), partPlmId, url);
            }
            item.put("partImage", partImage);
            item.put("partPlimd", parentWorkOrder != null ? parentWorkOrder.getMaterCode() : workOrder.getMaterCode());
            item.put("orderCompRate", String.format("%s/%s", materialFinishCount.setScale(0, RoundingMode.HALF_UP), materialTotalCount.setScale(0, RoundingMode.HALF_UP)));
            if (matchPhase(item, mapReq)) {
                continue;
            }

            result.add(item);
        }

        return result;

    }

    private boolean matchPhase(Map<String, Object> item, Map<String, Object> mapReq) {
        boolean flag = false;
        for (String s : mapReq.keySet()) {
            if (ObjectUtil.isNotEmpty(item.get(s))) {
                if (String.valueOf(item.get(s)).contains("/")) {
                    String data = (String) item.get(s);
                    String[] split = StringUtils.split(data, "/");
                    if (Integer.parseInt((String) mapReq.get(s)) == 0) {
                        if (!Objects.equals(split[0], split[1])) {
                            flag = true;
                        } else {
                            flag = false;
                        }
                    } else if (Integer.parseInt((String) mapReq.get(s)) == 1) {
                        if (Objects.equals(split[0], split[1])) {
                            flag = true;
                        } else {
                            flag = false;
                        }
                    } else if (Integer.parseInt((String) mapReq.get(s)) == 2) {
                        flag = false;
                    }
                } else {
                    flag = false;
                }
            } else {
                flag = true;
            }
        }
        return flag;
    }

    public OrderMonitorResVo getListNew(OrderMonitorReqVo reqVo) {

        Map<String, String> phaseName = phaseName();

        OrderMonitorResVo resVo = new OrderMonitorResVo();

        Map<Integer, Order> orders;

        if (StringUtils.hasText(reqVo.getOrderCode())) {
            // 查询施工单下的订单（输入施工号时）
            orders = orderMapper.lambdaQuery()
                    .like(Order::getConstructNo, reqVo.getOrderCode())
                    .in(Order::getOrderType,1, 3, 4)
                    .list()
                    .stream()
                    .collect(Collectors.toMap(Order::getId, Function.identity()));
            if (orders.isEmpty()) {
                throw SyExceptionUtils.e("未查询到该施工号的订单", reqVo.getOrderCode());
            }
        } else {
            if (StringUtils.hasText(reqVo.getPlmid())) {
                // 查询包含输入零件号的订单（不输入施工号，但输入零件号时）
                List<OrderBom> orderBom = orderBomMapper.lambdaQuery()
                        .like(OrderBom::getPlmId, reqVo.getPlmid())
                        .list();
                if (orderBom.isEmpty()) {
                    throw SyExceptionUtils.e("未查询到包含该零件的信息", reqVo.getPlmid());
                }
                orders = orderMapper.lambdaQuery()
                        .in(Order::getId, orderBom.stream().map(OrderBom::getOrderId).collect(Collectors.toList()))
                        .list().stream()
                        .collect(Collectors.toMap(Order::getId, Function.identity()));
            } else {
                // 查询最新施工单下的订单（不输入施工号，且不输入零件号时）
                orders = orderMapper.lambdaQuery()
                        .in(Order::getOrderType, 1, 3, 4)
                        .ge(Order::getOrderStatus, 6)
                        .orderByDesc(Order::getCreateDateTime)
                        .last("limit 1")
                        .list()
                        .stream()
                        .collect(Collectors.toMap(Order::getId, Function.identity()));
            }
        }
        if (orders.isEmpty()) { // 无订单
            return resVo;
        }

        // 查想订单相关的工单
        Map<Integer, ApsWorkOrder> workOrders = apsWorkOrderMapper.lambdaQuery()
                .in(ApsWorkOrder::getOrderId, orders.keySet())
                .in(ApsWorkOrder::getOrderType, 1, 3, 4)
                .list()
                .stream()
                .collect(Collectors.toMap(ApsWorkOrder::getId, Function.identity()));
        // 查询工单明细并进行聚合
        Map<List<String>, List<ApsWorkDetail>> map = apsWorkDetailMapper.lambdaQuery()
                .in(ApsWorkDetail::getOrderId, orders.keySet())
                .in(ApsWorkDetail::getOrderType, 1, 3, 4)
                .like(StringUtils.hasText(reqVo.getPlmid()), ApsWorkDetail::getMaterNo, reqVo.getPlmid())
                .list().stream()
                .filter(detail -> orders.containsKey(detail.getOrderId())
                        && workOrders.containsKey(detail.getApsWorkOrderId()))
                .collect(Collectors.groupingBy(detail -> {
                    Order order = orders.get(detail.getOrderId());
                    ApsWorkOrder workOrder = workOrders.get(detail.getApsWorkOrderId());
                    return Arrays.asList(order.getConstructNo(), workOrder.getWorkOrderCode(), detail.getMaterNo());
                }, Collectors.toList()));

        List<OrderMonitorInfoResVo> infoList = new ArrayList<>();

        List<OrderMonitorPhaseResVo> allPhaseList = getInitList();
        for (List<ApsWorkDetail> groupDetails : map.values()) {


            OrderMonitorInfoResVo info = new OrderMonitorInfoResVo();

            ApsWorkDetail detail = groupDetails.get(0);
            Order order = orders.get(detail.getOrderId());
            ApsWorkOrder workOrder = workOrders.get(detail.getApsWorkOrderId());

            resVo.setOrderCode(StringUtils.hasText(resVo.getOrderCode())?resVo.getOrderCode():order.getConstructNo());

            info.setId(detail.getId());
            info.setOrderCode(order.getConstructNo());
            info.setWorkOrderCode(workOrder.getWorkOrderCode());
            info.setPlmid(detail.getMaterNo());
            info.setMaterName(detail.getMaterName());
            ApsWorkOrder parentWorkOrder = workOrders.get(workOrder.getPid());
            Map<String, List<ApsWorkDetail>> phaseDetailMap = groupDetails.stream()
//                    .sorted(Comparator.comparing(ApsWorkDetail::getPhaseSeq))
                    .collect(Collectors.groupingBy(ApsWorkDetail::getPhaseCode, Collectors.toList()));
            BigDecimal materialFinishCount = BigDecimal.ZERO;
            BigDecimal materialTotalCount = BigDecimal.ZERO;
            // 获取最大工序序号的计划
            ApsWorkDetail max = groupDetails.stream().max(Comparator.comparing(e -> e.getPhaseSeq())).get();
//            List<ApsWorkDetail> list = new ArrayList<>(phaseDetails);
//            Collection<ApsWorkDetail> values = list.stream().filter(e -> e.getPhaseSeq().equals(apsWorkDetail.getPhaseSeq())).collect(Collectors.toMap(e -> e.getPhaseSeq(), e -> e, (e1, e2) -> {
//                e1.setTotalCount(e1.getTotalCount().add(e2.getTotalCount()));
//                e1.setFinishCount(e1.getFinishCount().add(e2.getFinishCount()));
//                return e1;
//            })).values();
//            ApsWorkDetail workDetail = new ArrayList<>(values).get(0);

            ApsWorkDetail last = null;

            List<OrderMonitorPhaseResVo> phaseList = new ArrayList<>();
            for (List<ApsWorkDetail> phaseDetails : phaseDetailMap.values()) {
                OrderMonitorPhaseResVo phaseInfo = new OrderMonitorPhaseResVo();

                // 获取最大工序序号的计划
                ApsWorkDetail apsWorkDetail = phaseDetails.stream().max(Comparator.comparing(e -> e.getPhaseSeq())).get();
                // 不修改原List，创建新List赋值
                List<ApsWorkDetail> list = new ArrayList<>(phaseDetails);
                // 按工序的序号，将数量进行累加，合并到第一条数据上
                Collection<ApsWorkDetail> values = list.stream().filter(e -> e.getPhaseSeq().equals(apsWorkDetail.getPhaseSeq())).collect(Collectors.toMap(e -> e.getPhaseSeq(), e -> e, (e1, e2) -> {
                    e1.setTotalCount(e1.getTotalCount().add(e2.getTotalCount()));
                    e1.setFinishCount(e1.getFinishCount().add(e2.getFinishCount()));
                    return e1;
                })).values();
                // 获取其中的第一条数据（只有一条，但是类型是Collection，强转为List后获取第一条）
                ApsWorkDetail workDetail = new ArrayList<>(values).get(0);

                if (Objects.equals(workDetail.getPhaseSeq(), max.getPhaseSeq())) {
                    // 如果是工序序号最大的一个，则赋值给last，用于零件或组件的订单完成率
                    last = workDetail;
                    materialFinishCount = last.getFinishCount();
                    materialTotalCount = last.getTotalCount();
                }
                // 获取零件或组件的各个工序完成数量和总数量
                BigDecimal finishCount = workDetail.getFinishCount();
                BigDecimal totalCount = workDetail.getTotalCount();
                phaseInfo.setPhaseCode(phaseDetails.get(0).getPhaseCode());
                phaseInfo.setPhaseComp(String.format("%s/%s", finishCount.setScale(0, RoundingMode.HALF_UP), totalCount.setScale(0, RoundingMode.HALF_UP)));
                phaseList.add(phaseInfo);
            }
            Map<String, OrderMonitorPhaseResVo> phaseCodeMap = phaseList.stream().collect(Collectors.toMap(OrderMonitorPhaseResVo::getPhaseCode, e -> e));

            List<OrderMonitorPhaseResVo> collect = allPhaseList.stream().map(e -> {
                OrderMonitorPhaseResVo newPhase = new OrderMonitorPhaseResVo();
                newPhase.setPhaseCode(e.getPhaseCode());
                newPhase.setPhaseName(phaseName.get(newPhase.getPhaseCode()));
                OrderMonitorPhaseResVo hasPhase = phaseCodeMap.get(e.getPhaseCode());
                if (hasPhase != null) {
                    newPhase.setPhaseComp(hasPhase.getPhaseComp());
                }

                return newPhase;
            }).collect(Collectors.toList());

            info.setPhaseList(collect);

            // 获取图纸信息
            OrderPart orderPart = orderPartMapper.lambdaQuery().eq(OrderPart::getPlmId, detail.getMaterNo()).one();
            String partImage = null;
            if (orderPart != null && orderPart.getPngUrl() != null) {
                String partPlmId = orderPart.getPlmId();
                String url = orderPart.getPngUrl();
                partImage = String.format(projectConfig.getUrlPre(), partPlmId, url);
            }
            info.setPartImage(partImage);
            info.setPartPlimd(parentWorkOrder != null ? parentWorkOrder.getMaterCode() : workOrder.getMaterCode());
            info.setOrderCompRate(String.format("%s/%s", materialFinishCount.setScale(0, RoundingMode.HALF_UP), materialTotalCount.setScale(0, RoundingMode.HALF_UP)));
            if (matchPhase(info, reqVo)) {
                continue;
            }

            infoList.add(info);

        }

        infoList.sort(Comparator.comparing(OrderMonitorInfoResVo::getPlmid));
        resVo.setInfo(infoList);
        return resVo;

    }

    private List<OrderMonitorPhaseResVo> getInitList() {
        List<String> arr = Arrays.asList("LL", "YC", "HG", "JG", "FJ", "JP", "JP01", "PK", "PK01", "GC", "ZW", "ZW01", "BX", "CX", "BT", "BZ", "XC", "PD", "YM", "PP", "YR", "PZ", "PH", "LH", "BH", "ZH", "ZT", "ZZ", "XH", "QM", "PT", "RH", "WH", "TS", "HJ", "KJ");
        return arr.stream()
                .map(e->{
                    OrderMonitorPhaseResVo resVo = new OrderMonitorPhaseResVo();
                    resVo.setPhaseCode(e);
                    return resVo;
                })
                .collect(Collectors.toList());
    }

    private boolean matchPhase(OrderMonitorInfoResVo info, OrderMonitorReqVo reqVo) {
        boolean flag = false;
        List<OrderMonitorPhaseReqVo> phaseReqList = reqVo.getPhaseStates();
        if (CollUtil.isEmpty(phaseReqList)) {
            return flag;
        }
        Map<String, OrderMonitorPhaseReqVo> phaseReqMap = phaseReqList.stream().collect(Collectors.toMap(OrderMonitorPhaseReqVo::getPhaseCode, e -> e));
        List<String> phaseCodeList = phaseReqList.stream().map(OrderMonitorPhaseReqVo::getPhaseCode).collect(Collectors.toList());

        List<OrderMonitorPhaseResVo> phaseInfoList = info.getPhaseList();
        Map<String, OrderMonitorPhaseResVo> phaseInfoMap = phaseInfoList.stream().collect(Collectors.toMap(OrderMonitorPhaseResVo::getPhaseCode, e -> e));
        for (String s : phaseCodeList) {
            if (!StringUtils.hasText(s)) {
                continue;
            }
            if (ObjectUtil.isNotEmpty(phaseInfoMap.get(s)) && StringUtils.hasText(phaseInfoMap.get(s).getPhaseComp())) {
                if (phaseInfoMap.get(s).getPhaseComp().contains("/")) {
                    String data = phaseInfoMap.get(s).getPhaseComp();
                    String[] split = StringUtils.split(data, "/");
                    if (phaseReqMap.get(s).getPhaseState() == 0) {
                        if (!Objects.equals(split[0], split[1])) {
                            flag = true;
                        } else {
                            flag = false;
                        }
                    } else if (phaseReqMap.get(s).getPhaseState() == 1) {
                        if (Objects.equals(split[0], split[1])) {
                            flag = true;
                        } else {
                            flag = false;
                        }
                    } else if (phaseReqMap.get(s).getPhaseState() == 2) {
                        flag = false;
                    }
                } else {
                    flag = false;
                }
            } else {
                flag = true;
            }
            if (flag) {
                return flag;
            }
        }
        return flag;
    }

    private Map<String, Object> getInitMap() {
        Map<String, Object> mapRes = new LinkedHashMap<>();
        mapRes.put("orderCode", null);
        mapRes.put("workOrderCode", null);
        mapRes.put("plmid", null);
        mapRes.put("materName", null);
        mapRes.put("partImage", null);
        mapRes.put("partPlimd", null);
        mapRes.put("orderCompRate", null);
        mapRes.put("LL", null);
        mapRes.put("YC", null);
        mapRes.put("HG", null);
        mapRes.put("JG", null);
        mapRes.put("FJ", null);
        mapRes.put("JP", null);
        mapRes.put("JP01", null);
        mapRes.put("PK", null);
        mapRes.put("PK01", null);
        mapRes.put("GC", null);
        mapRes.put("ZW", null);
        mapRes.put("ZW01", null);
        mapRes.put("BX", null);
        mapRes.put("CX", null);
        mapRes.put("BT", null);
        mapRes.put("BZ", null);
        mapRes.put("XC", null);
        mapRes.put("PD", null);
        mapRes.put("YM", null);
        mapRes.put("PP", null);
        mapRes.put("YR", null);
        mapRes.put("PZ", null);
        mapRes.put("PH", null);
        mapRes.put("LH", null);
        mapRes.put("BH", null);
        mapRes.put("ZH", null);
        mapRes.put("ZT", null);
        mapRes.put("ZZ", null);
        mapRes.put("XH", null);
        mapRes.put("QM", null);
        mapRes.put("PT", null);
        mapRes.put("RH", null);
        mapRes.put("WH", null);
        mapRes.put("TS", null);
        mapRes.put("HJ", null);
        mapRes.put("KJ", null);
        return mapRes;
    }

    private Map<String, String> phaseName() {
        Map<String, String> phaseName = new LinkedHashMap<>();
        phaseName.put("LL", "领料");
        phaseName.put("YC", "预处理");
        phaseName.put("HG", "火焰切割");
        phaseName.put("JG", "激光切割");
        phaseName.put("FJ", "分拣");
        phaseName.put("JP", "矫平");
        phaseName.put("JP01", "矫平01");
        phaseName.put("PK", "坡口");
        phaseName.put("PK01", "坡口01");
        phaseName.put("GC", "割加长");
        phaseName.put("ZW", "折弯");
        phaseName.put("ZW01", "折弯01");
        phaseName.put("BX", "板加铣边");
        phaseName.put("CX", "板加铣侧");
        phaseName.put("BT", "板加镗");
        phaseName.put("BZ", "板加钻");
        phaseName.put("XC", "铣长槽");
        phaseName.put("PD", "拼点");
        phaseName.put("YM", "预埋");
        phaseName.put("PP", "配盘");
        phaseName.put("YR", "预热");
        phaseName.put("PZ", "拼装");
        phaseName.put("PH", "平焊");
        phaseName.put("LH", "立焊");
        phaseName.put("BH", "背焊");
        phaseName.put("ZH", "整加划");
        phaseName.put("ZT", "整加镗");
        phaseName.put("ZZ", "整加钻");
        phaseName.put("XH", "小件拼焊");
        phaseName.put("QM", "清磨");
        phaseName.put("PT", "喷涂");
        phaseName.put("RH", "人工焊");
        phaseName.put("WH", "焊接完工");
        phaseName.put("TS", "无损探伤");
        phaseName.put("HJ", "焊缝外观检测");
        phaseName.put("KJ", "库检");
        return phaseName;
    }

    public BaseListResVo<OrderMonitorPalletResVo> getPallet(BaseIdReqVo reqVo) {

        List<OrderMonitorPalletResVo> list = partPalletMapper.selectPalletInfo(reqVo.getId());

        if (list.isEmpty()) {
            return BaseListResVo.empty();
        } else {
            for (OrderMonitorPalletResVo vo : list) {
                if (!org.springframework.util.StringUtils.hasText(vo.getPalletNo())) {
                    continue;
                }
                Map<String, Object> map = wmsFeignApiClient.getPositionByTrayId(vo.getPalletNo());
                if (!map.get("code").equals(0)) {
                    vo.setPosition(vo.getMaterNo() + "未查询到位置信息。");
                } else {
                    String position = (String) map.get("data");
                    if (position.equals("TransportBuffer")) {
                        vo.setPosition("托盘" + vo.getPalletNo() + "不在位置上。");
                    } else {
                        vo.setPosition(position);
                    }
                }
            }
            return BaseListResVo.of(list, OrderMonitorPalletResVo.class);
        }

    }
}
